package com.me.jingdongshop.ui.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.me.common.base.Global;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.HotproductBean;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.ui.activity.GoodsDetailActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuer on 2017/5/8/008.
 * 热门单品类的适配器
 */

public class HomeHotItemAdapter extends RecyclerView.Adapter<HomeHotItemAdapter.ViewHolder> {
    private RecyclerView recyclerView;
    private List<HotproductBean.ProductListBean> mProductListBeen = new ArrayList<>();

    public HomeHotItemAdapter(List<HotproductBean.ProductListBean> productListBeen) {
        super();
        this.mProductListBeen = productListBeen;
    }

    @Override
    public HomeHotItemAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_rv_hot_item, viewGroup, false);
        HomeHotItemAdapter.ViewHolder vh = new HomeHotItemAdapter.ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(HomeHotItemAdapter.ViewHolder holder, final int position) {
        RecyclerView.ViewHolder viewHolder = holder;
        String pic = mProductListBeen.get(position).getPic();
        Picasso.with(Global.mContext).load(IHttpService.HOST_URL + pic).into(holder.ivHotItem);
        String name = mProductListBeen.get(position).getName();
        holder.tvName.setText(name);
        //删除线
        holder.tvMarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        int marketPrice = mProductListBeen.get(position).getMarketPrice();
        holder.tvMarketPrice.setText("￥" + marketPrice + "");
        int price = mProductListBeen.get(position).getPrice();
        holder.tvPrice.setText("￥" + price + "");
        //调转到商品详情
        holder.llHomeHotItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), GoodsDetailActivity.class);
                intent.putExtra("id",mProductListBeen.get(position).getId());
                v.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mProductListBeen == null ? 0 : mProductListBeen.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout llHomeHotItem;
        private ImageView ivHotItem;
        private TextView tvName;
        private TextView tvMarketPrice;
        private TextView tvPrice;

        public ViewHolder(View view) {
            super(view);
            llHomeHotItem = (LinearLayout) view.findViewById(R.id.ll_home_hot_item);
            ivHotItem = (ImageView) view.findViewById(R.id.iv_hot_item);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvMarketPrice = (TextView) view.findViewById(R.id.tv_Market_price);
            tvPrice = (TextView) view.findViewById(R.id.tv_price);

        }
    }

}
